//
//  SignRedVc.swift
//  TianTianBiTing
//
//  Created by Gox mac 2 on 2019/2/28.
//  Copyright © 2019 Gox_WallyDiao. All rights reserved.
//

import UIKit

class SignRedVc: TTBaseVC {
    let SignRedTableViewCellID = "SignRedTableViewCell"
    
    lazy var listenModel: [SignInListModel] = [{
        return SignInListModel()
        }()]
    lazy var headView:SignRedHeadView = {
        let header = SignRedHeadView.init(frame: FRAME_CGFloat(0, 0, SCREEN_WIDTH, 155))
        header.checkBlock = { () in
            let vc = TBWithdrawVc()
            PUSHVIEWCONTROLLER(vc)
        }
        return header
    }()
    
    lazy var backView:EveryDaySignView = {
        let view = EveryDaySignView.init(frame: FRAME_CGFloat(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - OFFSET_TOP))
        view.checkBtn.isHidden = true
        view.signBlock = { [unowned self] in
            TTAlertView.showMessage(message: "签到成功")
            self.headView.setMoneyData()
            self.setData()
        }
        view.isHidden = true
        return view
    }()
    
    private lazy var tableView: TTTableView = {
        let tableView = TTTableView.init(frame: FRAME_CGFloat(0, 185, SCREEN_WIDTH, SCREEN_HEIGHT - OFFSET_TOP - 185), style: UITableView.Style.plain)
        tableView.delegate = self
        tableView.dataSource = self
        tableView.backgroundColor = WhiteBackColor
        tableView.separatorStyle = UITableViewCell.SeparatorStyle.none
        tableView.showsVerticalScrollIndicator = false
        tableView.showsHorizontalScrollIndicator = false
        tableView.register(SignRedTableViewCell.self, forCellReuseIdentifier: SignRedTableViewCellID)
        tableView.tableFooterView = UIView.init(frame: .zero)
        tableView.isScrollEnabled = false
        return tableView
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.title = "签到拿红包"
        
        setHeaderView()
        self.view.addSubview(self.tableView)
        self.view.addSubview(self.backView)
        
        setData()
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super .viewWillAppear(animated)
        setBackData()
    }
    
    deinit {
        print("fasdfasdf")
    }

}

// MARK: *******  UI  ********
extension SignRedVc {
    func setHeaderView() {
        self.view.addSubview(self.headView)
    }
}

//  MARK: - ###### 方法 ######
extension SignRedVc {
    func setData() {
        WelfareRequestModel.loadSignInListData(parameters: ["userId":String.getUserId(), "flg":1]) { (success, message, model) in
            if success && model != nil {
                self.listenModel = model!
                self.tableView.reloadData()
            }
        }
    }
    
    func setBackData() {
        WelfareRequestModel.loadCurrentDaySignInData { (success) in
            if success {
                self.backView.isHidden = true
            } else {
                self.backView.isHidden = false
            }
        }
    }
    
}

//  MARK: - ###### 协议 ######
extension SignRedVc: UITableViewDelegate, UITableViewDataSource {
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.listenModel.count

    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 91.5
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: SignRedTableViewCellID) as! SignRedTableViewCell
        cell.isUserInteractionEnabled = false
        cell.backgroundColor = UIColor.clear
        cell.modelList = self.listenModel[indexPath.row]
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
    }
}
